package com.swehr.servlet;

import com.swehr.common.StringUtil;
import com.swehr.model.Organization;
import com.swehr.proxy.PersonnelServiceProxy;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Rock Huang
 * @version 0.1
 */
public class SignUpServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
        String serv = req.getServletPath();
        if (serv.equals("/")) {
            req.getRequestDispatcher("/home.ftl").forward(req, resp);
        } else if (serv.equalsIgnoreCase("/signout")) {
            Cookie c = new Cookie("email", "");
            c.setMaxAge(-1);
            resp.addCookie(c);
            resp.sendRedirect("signin");
        } else if (serv.equalsIgnoreCase("/signin")) {
            req.setAttribute("signin", true);
            req.getRequestDispatcher("signin.ftl").forward(req, resp);
        } else if (serv.equalsIgnoreCase("/signup")) {
            req.getRequestDispatcher("signup.ftl").forward(req, resp);
        } else {
            resp.sendRedirect("404.ftl");
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
        String serv = req.getServletPath();

        if (serv.equalsIgnoreCase("/signin")) {
            String email = req.getParameter("email");
            String password = req.getParameter("password");
            Organization org = new Organization();
            org.setEmail(email);
            org.setPassword(password);
            org = new PersonnelServiceProxy().signin(org);
            if (org.getId() > 0) {
                resp.addCookie(new Cookie("email", org.toString()));
                req.getRequestDispatcher("dashboard.ftl").forward(req, resp);
            } else {
                req.setAttribute("signin", true);
                req.setAttribute("flash", "Sorry, didn't found the account. Please double check the email and password.");
                req.getRequestDispatcher("signin.ftl").forward(req, resp);
            }
        } else if (serv.equalsIgnoreCase("/signup")) {
            String website = req.getParameter("website");
            String name = req.getParameter("name");
            String email = req.getParameter("email");
            String password = req.getParameter("password");

            if (StringUtil.isEmptyOrNull(name) || StringUtil.isEmptyOrNull(email)
                    || StringUtil.isEmptyOrNull(password)) {
                req.setAttribute("flash", "All fields are required");
                req.getRequestDispatcher("signup.ftl").forward(req, resp);
                return;
            }

            Organization org = new Organization();
            org.setWebsite(website);
            org.setName(name);
            org.setEmail(email);
            org.setPassword(password); // todo: password needs encryption.
            org = new PersonnelServiceProxy().add(org);
            if (org.getId() > 0) {
                req.setAttribute("status", true);
                req.setAttribute("flash", "Your account is ready, please <a href='/'>sign in</a>");
            } else {
                req.setAttribute("flash", "Sorry, we have broken something on our system. Please try later");
            }
            req.getRequestDispatcher("signup.ftl").forward(req, resp);
        } else {
            resp.sendRedirect("404.ftl");
        }
    }

    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
    }

    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
    }
}
